package HashTable;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.*;
import java.util.Set;

public class lc128 {
    public int longestConsecutive(int[] nums) {
        //将所有数字去重(放入hashset)
        HashSet<Integer> set=new HashSet<>();
        for (int val:nums) {
            set.add(val);
        }
        int max_length=0;
        for (int val:set) {
            //对于每一个数字从它开始 x+1 x+2.....
            if(!set.contains(val-1)){
                int cur=val;
                int length=0;
                while(set.contains(val)){
                    cur++;
                    length++;
                }
                if(length>max_length){
                    max_length=length;
                }
                }
            }
        return max_length;
    }

    public int longestConsecutive1(int[] nums) {
        //将所有数字去重(放入hashset)
        HashSet<Integer> set=new HashSet<>();
        for (int val:nums
             ) {
            set.add(val);
        }
        int max_length=1;
        int cur_length=1;
        for (int val:set
             ) {
            int num=val;
            while (set.contains(num+1)){
                cur_length++;
                num++;
            }
            if(cur_length>max_length){
                max_length=cur_length;
            }
            cur_length=1;
        }

       return max_length;
    }
}
